getwd()
setwd("J:/gov2001/Data")

library("foreign")
mydata <- read.dta("J:/gov2001/Data/amelia.dta")
names(mydata)
require(Amelia)
names(mydata)

a.out <- amelia(mydata, m=5, ts="year", cs="newleaid", idvars = c("refid"))


texp1 <- ols(totalexp_pp ~ win_1 + win_2 + win_3 + win_4 + win_5 + win_6 + factor(year) + factor(refid) + dydums1 + dydums2 + 
dydums3 + dydums4 + dydums5 + dydums6 + dydums7 + dydums8 + dydums9 + req_1 + req_2 + req_3 + req_4 + req_5 + req_6 + 
percent_1 + percent_2 + percent_3 + percent_4 + percent_5 + percent_6 + percent2_1 + percent2_2 + percent2_3 + percent2_4 + 
percent2_5 + percent2_6 + percent3_1 + percent3_2 + percent3_3 + percent3_4 + percent3_5 + percent3_6, data = GOB)










imput1 <- a.out$imputations[[1]]
imput2 <- a.out$imputations[[2]]
imput3 <- a.out$imputations[[3]]
imput4 <- a.out$imputations[[4]]
imput5 <- a.out$imputations[[5]]


write.dta(imput1, file="imput1.dta")
write.dta(imput2, file="imput2.dta")
write.dta(imput3, file="imput3.dta")
write.dta(imput4, file="imput4.dta")
write.dta(imput5, file="imput5.dta")
###########################################################
?
#old code and code from Ashley


missmap(a.out)

### this code outputs data in CSV file format 
write.amelia(obj=a.out, file.stem==", format = "dta")
write.amelia(obj=a.out, file.stem = "outdata", format = "dta")
plot(a.out, which.vars = 5:9)







#########################################################################
##Reading in the original dataset for imputation
data <- read.csv("C:/Documents and Settings/createdefault/owomen.csv")]
require(Amelia)

##imputing the original dataset
a.out <- amelia(data, m=5, ts="year", cs="cty", idvars = c("id","id2"), sqrts = c("oil_gas_rentPOP", "laborfemale", "logGDPcap", "logGDPcap_sq", "age15_64"))


##looking to see if the imputation converged
a.out

##imputation diagnostics
plot(a.out)
compare.density(a.out, var="laborfemale")

overimpute(a.out, var="laborfemale")

disperse(a.out, dims=1, m=5)

tscsPlot(a.out, cs="Armenia", var = "logGDPcap")








##Reading in the modified dataset for imputation

data1 <- read.csv("C:/Documents and Settings/createdefault/newwomens.csv")

##imputing the modified dataset

a.out1 <- amelia(data1, m=5, ts="year", cs="cty", idvars = c("id","id2"), sqrts = c("oil_gas_rentPOP", "laborfemale", "logGDPcap", "logGDPcap_sq", "age15_64"))

##checking to see if the dataset converged
a.out1

##imputation diagnostics

plot(a.out1, which.vars = 5:9)

##I'm ok with the fit of the GDP vars, and am somewhat ok with the fit on the age var, but I don't really like the labor var. I'm not sure that it matters that much though
##because only a bit of the dataset (approx 139 obs) are missing to begin with)


##missingness map
missmap(a.out1)

##I really don't know what this even does so I'm not concerned


##Checking for overimputation
overimpute(a.out1, var="laborfemale")


## I can't read this graph... too many datapoints to be coherent


##Making sure that my imputations created viable datasets
a.out1$imputations[[1]]


##Turning the datasets into CSV files so that I may recode the data (first differences and normalization) and run the models.

womdata1 <- a.out1$imputations[[1]]

head(womdata1)

write.csv(womdata1, file="womdata1.csv")

womdata2 <- a.out1$imputations[[2]]

head(womdata2)

write.csv(womdata2, file="womdata2.csv")


womdata3 <- a.out1$imputations[[3]]

head(womdata3)

write.csv(womdata3, file="womdata3.csv")


womdata4 <- a.out1$imputations[[4]]

head(womdata4)

write.csv(womdata4, file="womdata4.csv")


womdata5 <- a.out1$imputations[[5]]

head(womdata5)

write.csv(womdata1, file="womdata5.csv")



##imputing the modified dataset

a.out3 <- amelia(data1, m=5, ts="year", cs="cty", idvars = c("id","id2"), polytime=3, sqrts = c("oil_gas_rentPOP", "laborfemale", "logGDPcap", "logGDPcap_sq", "age15_64"))

##checking to see if the dataset converged
a.out2

##imputation diagnostics

plot(a.out1, which.vars = 5:9)

plot(a.out2, which.vars = 5:9)

plot(a.out3, which.vars = 5:9)

plot(a.out4, which.vars = 5:9)


womdats1 <- a.out4$imputations[[1]]

head(womdats1)

write.csv(womdats1, file="womdats1.csv")